/*eslint-disable */
function authorizationBtn ($rootScope) {
  'ngInject';

  return {
    multiElement: true,
    restrict: 'A',
    link: linkFn,
  };

  function linkFn(scope, elem, attrs, ctrl, $rootScope) {
    const restUrl = attrs.authorizationBtn;

    function isAuthenticatedBtn(restUrl, permissionList) {
      for (let i = 0; i < permissionList.length; i++) {
        const authenticatedUrl = permissionList[i].url;
        if (authenticatedUrl.indexOf(restUrl) !== -1) {
          return true;
        }
      }
      return false;
    }

    if (scope.permissionList && !isAuthenticatedBtn(restUrl, scope.permissionList)) {
      elem.remove();
    }
  }
}

export default authorizationBtn;
